package com.shadt.cloud.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shadt.cloud.service.BranchChannelService;
import com.shadt.cloud.service.RecordService;
import com.shadt.cloud.vo.RecordVo;
import com.shadt.core.controller.BaseController;
import com.shadt.core.model.Json;

/**
 * 记录控制器
 * 
 * @author SongR
 *
 */
@Controller
@RequestMapping(value = "/i/record")
public class RecordController extends BaseController {

	Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	RecordService service;

	@Autowired
	BranchChannelService bcservice;
	
	/**
	 * 根据key获取记录
	 * 
	 * @param appname
	 * @param keys
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/get", method = { RequestMethod.GET, RequestMethod.POST })
	public Json get(String appname,String key, String id, Long size) {
		Json j = new Json();
		Map<String, List<RecordVo>> m = new HashMap<String, List<RecordVo>>();
		if(bcservice.check(key, appname)){
			if (null != key && !"".equals(key)) {
				try {
					List<RecordVo> vos = service.get(appname,key, id, size);
					if(vos.size()>0){
						m.put(key, vos);
						j.setMsg("成功获取记录！");
						j.setObj(m);
					}else{
						j.setMsg("暂无记录！");
					}
					j.setSuccess(true);
				} catch (Exception e) {
					e.printStackTrace();
					j.setSuccess(false);
					j.setMsg("接口异常！");
					log.error(e.toString(),e);
				}
			}else{
				j.setSuccess(false);
				j.setMsg("key值不能为空！");
			}
		}else{
			j.setSuccess(false);
			j.setMsg("此平台没有此栏目的数据获取权限，请申请！");
		}
		return j;
	}

	/**
	 * 
	 * @param appname 分平台名称
	 * @param key	  权限关键字
	 * @param startTime 开始时间
	 * @param endTime	结束时间
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getlist", method = { RequestMethod.GET, RequestMethod.POST })
	public Json getList(String appname,String key,String startTime ,String endTime,Long size,String id){
		Json j = new Json();
		Map<String, List<RecordVo>> m = new HashMap<String, List<RecordVo>>();
		if(bcservice.check(key, appname)){
			if (null != key && !"".equals(key)) {
				try {
					m.put(key,service.get(key,startTime,endTime,size,id));
					j.setSuccess(true);
					j.setObj(m);
					j.setMsg("成功获取记录！");
				} catch (Exception e) {
					e.printStackTrace();
					j.setSuccess(false);
					j.setMsg("接口异常！");
					log.error(e.toString(),e);
				}
			}else{
				j.setSuccess(false);
				j.setMsg("key值不能为空！");
			}
		}else{
			j.setSuccess(true);
			j.setMsg("此平台没有此栏目的数据获取权限，请申请！");
		}
		return j;
	}
}
